package com.smallcc.handler;

import com.smallcc.config.ProjectUrlConfig;
import com.smallcc.exception.ResponseBankException;
import com.smallcc.exception.SellException;
import com.smallcc.exception.SellerAuthorizeException;
import com.smallcc.util.ResultVoUtil;
import com.smallcc.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 晓风过境
 * @TODO 拦截登录异常
 * @date 2018/4/3 22:11
 * @day 03
 * @return $text$
 * @title ${CLASS_NAME}
 * @project_name sell
 */
@ControllerAdvice
public class SellExceptionHandler {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;
    // /拦截登录异常
    //http://sell.springboot.cn/sell/wechat/qrAuthorize?returnUrl=http://sell.springboot.cn/sell/seller/login
    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView("redirect:"
                .concat(projectUrlConfig.getWechatOpenAuthorize())
                .concat("/sell/wechat/qrAuthorize")
                .concat("?returnUrl=")
                .concat(projectUrlConfig.getSell())
                .concat("/sell/seller/login"));
    }
    //异常捕获
    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVO handlerSellerException(SellException e){
        return ResultVoUtil.error(e.getCode(),e.getMessage());
    }


    //捕获银行错误
    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void responseBankException(SellException e){
    }
}
